#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
__title__ = ''
__author__ = 'tinxi'
__mtime__ = '2018/6/23'
# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
    
'''

from django import forms
# from django.shortcuts import render, redirect, reverse
from django.contrib import messages
from .models import User
from apps.forms import FormsMixin


class LoginForm(forms.Form, FormsMixin):
    telephone = forms.CharField(min_length=11, max_length=11,
                                error_messages={'required': '必须输入手机号码!', 'min_length': '手机号码必须为11位',
                                                'max_length': '手机号码个数必须为11位'})
    password = forms.CharField(min_length=6, max_length=20,
                               error_messages={'required': '必须输入密码!', 'min_length': '密码最少不能少于6位',
                                               'max_length': '密码最长不能超过20位'})
    remember = forms.IntegerField(required=False)


class RegisterForm(forms.Form, FormsMixin):
    telephone = forms.CharField(min_length=11, max_length=11,
                                error_messages={'required': '必须输入手机号码!', 'min_length': '手机号码必须为11位',
                                                'max_length': '手机号码个数必须为11位'})
    username = forms.CharField(max_length=20, min_length=5,
                               error_messages={'required': '必须输入用户名!', 'max_length': '用户名最长为20位',
                                               'min_length': '用户名最少5位'})
    password1 = forms.CharField(min_length=6, max_length=20,
                                error_messages={'required': '必须输入密码!', 'min_length': '密码最少不能少于6位',
                                                'max_length': '密码最长不能超过20位'})
    password2 = forms.CharField(min_length=6, max_length=20,
                                error_messages={'required': '必须输入密码!', 'min_length': '密码最少不能少于6位',
                                                'max_length': '密码最长不能超过20位'})
    sms_captcha = forms.CharField(max_length=5, min_length=5, error_messages={'required': '必须输入手机验证码!'})
    img_captcha = forms.CharField(max_length=5, min_length=5, error_messages={'required': '必须输入验证码!'})

    # 调用 id_valid()方法,会调用forms的clean()方法,所有字段验证
    # def clean(self):
    #     pass

    def validate_data(self, request):
        cleaned_data = self.cleaned_data
        password1 = cleaned_data.get('password1')
        password2 = cleaned_data.get('password2')

        if password1 != password2:
            messages.info(request,'两次密码输入不一致!')
            print('第一次密码:', password1)
            print('第二次密码', password2)
            return self.add_error('password1', '两次输入密码不一致!')

        img_captcha = cleaned_data.get('img_captcha')
        req_img = request.session.get('img_captcha')
        if req_img and img_captcha.lower() != req_img.lower():
            print('原始图形验证码:', img_captcha)
            print('输入验证码:', req_img)
            return self.add_error('img_captcha', '图形验证码错误!')

        sms_captcha = cleaned_data.get('sms_captcha')
        req_sms = request.session.get('sms_captcha')
        if req_sms:
            if sms_captcha.lower() != req_sms.lower():
                print('原始验证码:', sms_captcha)
                print('输入手机验证码:', req_sms)
                return self.add_error('sms_captcha', '手机验证码错误!')
        else:
            return self.add_error('sms_captcha', '手机验证码发送失败!')

        # 验证用户是否存在
        telephone = cleaned_data.get('telephone')
        exist_user = User.objects.filter(telephone=telephone)
        if exist_user:
            print('注册手机号码:', telephone)
            return self.add_error('telephone', '该用户已存在!')

        return True
